//
//  SZLShopViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/26.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLShopViewController.h"
#import "NormalShopTableViewCell.h"
#import <MJRefresh.h>
#import "SingleNetService.h"
#import "MBProgressHUD+MJ.h"
#import <Masonry.h>

@interface SZLShopViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak) UIButton *lastSelectBtn;
@property (nonatomic,weak) UIView *toolBar;
@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *sources;
@property (nonatomic,weak) UILabel *troductionLab;

@end

@implementation SZLShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商铺";
    [self setView];
    self.curPage = 1;
    [self getShopGoodsList];
    [self getShopTroduction];
}
- (void)setView
{
    [self setToolBar];
    [self.view addSubview:self.sourceTab];
    UILabel *troductionLab = [[UILabel alloc]init];
    troductionLab.font = Font(15);
    troductionLab.textColor = [UIColor blackColor];
    troductionLab.numberOfLines = 0;
    troductionLab.hidden = YES;
    self.troductionLab = troductionLab;
    [self.view addSubview:troductionLab];
    [troductionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(15);
    }];
}
- (void)setToolBar
{
    NSArray *titles = @[@"全部商品",@"简介"];
    CGFloat btnW = ScreenW/2.0;
    UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 40)];
    self.toolBar = toolBar;
    [self.view addSubview:toolBar];
    for (int i=0; i<2; i++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(btnW*i, 0, btnW, 40)];
        [btn setBackgroundColor:RGBColorOne(240)];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:WineColor forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(typeSelect:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [toolBar addSubview:btn];
        if (i==0) {
            btn.selected = YES;
            self.lastSelectBtn = btn;
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(btnW-1, 10, 1, 20)];
            lineView.backgroundColor = RGBColorOne(160);
            [toolBar addSubview:lineView];
        }
    }
}
- (void)typeSelect:(UIButton *)btn
{
    if (self.lastSelectBtn == btn) return;
    btn.selected = !btn.selected;
    self.lastSelectBtn.selected = NO;
    self.lastSelectBtn = btn;
    if (btn.tag == 0) {
        self.sourceTab.hidden = NO;
        self.troductionLab.hidden = YES;
    }else{
        self.sourceTab.hidden = YES;
        self.troductionLab.hidden = NO;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NormalShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NormalShopTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark --------------------请求---------------------
- (void)getShopGoodsList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"shopId"] = self.shopId;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [SingleNetService shopProductListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *gooods) {
        [self hideWaiter];
        [self hideRequestAgainView];
        [self.sourceTab.mj_header endRefreshing];
        [self.sourceTab.mj_footer endRefreshing];
        if (success) {
            (gooods.count<20)?(self.sourceTab.mj_footer.hidden) = YES:0;
            if (self.curPage == 1) {
                [self.sources removeAllObjects];
                if (gooods.count == 0) {
                    [self showNoDataView:self.view.frame title:@"暂无商品"];
                    return ;
                }
            }else{
                if (gooods.count == 0) {
                    self.curPage--;
                    return;
                }
            }
            [self.sources addObjectsFromArray:gooods];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getShopGoodsList)];
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}

- (void)getShopTroduction
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"shopId"] = self.shopId;
    [SingleNetService ShopTroductionWithParame:parame response:^(BOOL success, NSString *msg, ShopModel *model) {
        self.troductionLab.text = model.shopIntroduction;
        self.navigationItem.title = model.shopName;
    }];
}

- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.toolBar.frame), ScreenW, ScreenH-64-self.toolBar.height)];
        _sourceTab.dataSource = self;
        _sourceTab.delegate = self;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([NormalShopTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([NormalShopTableViewCell class])];
        _sourceTab.rowHeight = 120;
        _sourceTab.tableFooterView = [[UIView alloc]init];
        _sourceTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self getShopGoodsList];
        }];
        _sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getShopGoodsList];
        }];
    }
    return _sourceTab;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
